package com.course.cdspringboot.controller;


import com.course.cdspringboot.mapper.StudentMapper;
import com.course.cdspringboot.mapper.UserMapper;
import com.course.cdspringboot.security.RoleAccessControl;
import com.course.cdspringboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/student")
public class StudentController {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private RoleAccessControl roleAccessControl;
    @Autowired
    private UserMapper userMapper;


    // 获取所有学生列表
    @GetMapping("/all_students")
    public List<Map<String, Object>> getAllStudents() {
        roleAccessControl.checkAccess("ROLE_ADMIN", "ROLE_TEACHER");
        return studentMapper.getAllStudents();
    }

    // 获取自己的学生列表
    @GetMapping("/my_students")
    public List<Map<String, Object>> getMyStudents() {
        Long uid = roleAccessControl.checkAccess("ROLE_TEACHER");
        return studentMapper.getMyStudents((Long) uid);
    }
    // 学生获取自己的主页信息
    @GetMapping("/student_home_info")
    public Map<String, Object> getStudentInfo(){
        Long uid = roleAccessControl.checkAccess("ROLE_STUDENT");
        return studentMapper.getStudentHomeInfo(uid);
    }

    // 学生更新自己的详细信息
    @PatchMapping("/update_personal_info")
    public Result updatePersonalInfo(@RequestBody Map<String, Object> studentDetailInfo){
        Long uid = roleAccessControl.checkAccess("ROLE_STUDENT");
        studentDetailInfo.put("uid", uid);
        int change = studentMapper.updatePersonalInfo(studentDetailInfo);
        if (change > 0) {
            return Result.update_ok();
        }
        return Result.insert_ok().data("msg", "更新失败");
    }
}